package com.yf.exam.modules.trainingarchives.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.Delete;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yf.exam.common.enums.AuditCodeEnum;
import com.yf.exam.common.enums.DeleteCodeEnum;
import com.yf.exam.core.api.ApiError;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.core.enums.CommonState;
import com.yf.exam.core.exception.ServiceException;
import com.yf.exam.modules.sys.user.dto.SysUserDTO;
import com.yf.exam.modules.sys.user.dto.request.SysUserSaveReqDTO;
import com.yf.exam.modules.sys.user.entity.SysUser;
import com.yf.exam.modules.sys.user.service.SysUserService;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationAuditDTO;
import com.yf.exam.modules.trainingarchives.organization.dto.OrganizationDTO;
import com.yf.exam.modules.trainingarchives.organization.entity.Organization;
import com.yf.exam.modules.trainingarchives.organization.mapper.OrganizationMapper;
import com.yf.exam.modules.trainingarchives.organization.service.IOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.modules.trainingarchives.organization.vo.OrganizationVO;
import com.yf.exam.modules.user.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 机构管理表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements IOrganizationService {


    @Resource
    private SysUserService sysUserService;

    @Override
    public void beasVerify(String account,String licenseCode,String id) {
        Integer count =0;
        count = baseMapper.selectCount(Wrappers.<Organization>lambdaQuery().eq(Organization::getAccount, account).ne(Organization::getId,id).eq(Organization::getIsDeleted, DeleteCodeEnum.UN_DELETE.code).ne(Organization::getAuditState, AuditCodeEnum.NO_PASS_AUDIT.code));
        if(count > 0){
            throw new ServiceException(ApiError.ERROR_10010015);
        }
        count = baseMapper.selectCount(Wrappers.<Organization>lambdaQuery().eq(Organization::getLicenseCode, licenseCode).ne(Organization::getId,id).eq(Organization::getIsDeleted, DeleteCodeEnum.UN_DELETE.code).ne(Organization::getAuditState, AuditCodeEnum.NO_PASS_AUDIT.code));
        if(count > 0){
            throw new ServiceException(ApiError.ERROR_10010016);
        }
    }

    @Override
    public void add(OrganizationDTO dto) {
        Organization organization = new Organization();
        BeanUtils.copyProperties(dto, organization);
        organization.setAuditState(AuditCodeEnum.WAIT_AUDIT.code);
        this.save(organization);
    }

    @Override
    public void modify(OrganizationDTO reqDTO) {
        //保存机构信息
        Organization organization = new Organization();
        BeanUtils.copyProperties(reqDTO, organization);
        baseMapper.updateById(organization);
        //更新用户信息
        SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getDepartId, reqDTO.getId()), false);
        SysUserSaveReqDTO sysUserSaveReqDTO = new SysUserSaveReqDTO();
        sysUserSaveReqDTO.setUserName(reqDTO.getAccount());
        sysUserSaveReqDTO.setRealName(reqDTO.getName());
        sysUserSaveReqDTO.setPassword(reqDTO.getPassword());
        sysUserSaveReqDTO.setId(sysUser.getId());
        List<String> roles = new ArrayList<String>();
        roles.add("org");
        sysUserSaveReqDTO.setRoles(roles);
        sysUserService.save(sysUserSaveReqDTO);

    }

    @Override
    public void delete(String id) {
        Organization organization = new Organization();
        organization.setId(id);
        this.removeById(organization);
        //停用用户
        SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getDepartId, id), false);
        sysUser.setState(CommonState.ABNORMAL);
        sysUserService.updateById(sysUser);
    }

    @Override
    public void audit(OrganizationAuditDTO reqDTO) {
        Organization organization = new Organization();
        BeanUtils.copyProperties(reqDTO, organization);
        baseMapper.updateById(organization);
        organization = getById(reqDTO.getId());
        //审核通过，创建用户
        if (reqDTO.getAuditState() == AuditCodeEnum.PASS_AUDIT.code) {
            SysUserSaveReqDTO sysUserSaveReqDTO = new SysUserSaveReqDTO();
            sysUserSaveReqDTO.setUserName(organization.getAccount());
            sysUserSaveReqDTO.setRealName(organization.getName());
            sysUserSaveReqDTO.setPassword(organization.getPassword());
            sysUserSaveReqDTO.setDepartId(organization.getId());
            List<String> roles = new ArrayList<String>();
            roles.add("org");
            sysUserSaveReqDTO.setRoles(roles);
            sysUserService.save(sysUserSaveReqDTO);
        }
    }

    @Override
    public IPage<OrganizationVO> paging(PagingReqDTO<OrganizationVO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }

    @Override
    public List<OrganizationVO> getOrgList() {
        boolean admin = UserUtils.isAdmin(false);
        if(admin){
            return baseMapper.getOrgList("");
        }
        String departId = UserUtils.getDepartId();
        return baseMapper.getOrgList(departId);
    }
}
